//
//  PlaylistViewController.m
//  MusicPlayer-two
//
//  Created by lanou3g on 15/8/21.
//  Copyright (c) 2015年 大展宏图. All rights reserved.
//

#import "PlaylistViewController.h"
#import "PlaylistTableViewCell.h"
#import "PlayerViewController.h"
#import "PlayerManager.h"
#import "YiRefreshFooter.h"
#import "YiRefreshHeader.h"
#import "KVNProgress.h"

@interface PlaylistViewController ()< UITableViewDelegate, UITableViewDataSource >
@property (nonatomic, strong) YiRefreshFooter *refreshFooter;
@property (nonatomic, strong) YiRefreshHeader *refreshHeader;
@property (nonatomic, assign) NSInteger total;

@end

@implementation PlaylistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.playlist = [[Playlist alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = _playlist;
    
    self.playlist.playlistTableView.delegate = self ;
    self.playlist.playlistTableView.dataSource = self;
    
    /**
     *  cell的下划线的风格...
     */
    self.playlist.playlistTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
//    UITableViewCellSeparatorStyleSingleLine;
//    UITableViewCellSeparatorStyleNone;
    

    
    UIView *view = [[UIView alloc]initWithFrame:self.view.bounds];
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:self.view.bounds];
    imageView.image = [UIImage imageNamed:@"girl7.jpg"];
    [view addSubview:imageView];
    self.playlist.playlistTableView.backgroundView = view;
    
     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"back.jpg"] forBarMetrics:UIBarMetricsDefault];
    
        UIColor *color = [UIColor whiteColor];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    self.navigationController.navigationBar.titleTextAttributes = dic;
    self.title = @"播放列表";
    
//    [[PlayerManager defaultManager] getPlayerlistComplete:^{
//        [self.playlist.playlistTableView reloadData];
//    }];
    
    [[PlayerManager defaultManager]getPlayerlistComplete:^{
        
    
        [self.playlist.playlistTableView reloadData];

        
        
        
        
    }];
   
    //下拉刷新
    
    /*
    _total = 0;
    _refreshHeader = [[YiRefreshHeader alloc]init];
    _refreshHeader.scrollView = self.playlist.playlistTableView ;
    [_refreshHeader header];
    _refreshHeader.beginRefreshingBlock = ^(){
        
        [[PlayerManager defaultManager]getPlayerlistComplete:^{
           
                
                _total = 20;
            [self.playlist.playlistTableView reloadData];
                [_refreshHeader endRefreshing];
           
            
            
           
        }];
        
        
       
    };
    [_refreshHeader beginRefreshing];
  
     */
    //底部加载

    _refreshFooter = [[YiRefreshFooter alloc]init];
    _refreshFooter.scrollView = _playlist.playlistTableView;
    [_refreshFooter footer];
    
    _refreshFooter.beginRefreshingBlock = ^{
    
        [[PlayerManager defaultManager] getPlayerlistComplete:^{
            if (_total < 200) {
                
                _total = _total + 50;
                [_playlist.playlistTableView reloadData];
                
                [_refreshFooter endRefreshing];
            }
            
            
        }];
    };
    
}


- (void)viewWillAppear:(BOOL)animated{
    
    NSInteger index = [[PlayerManager defaultManager].playlist indexOfObject:[PlayerManager defaultManager].currentMusic];
    [self.playlist.playlistTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
   

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma  mark - playlistTableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSLog(@"%ld",[PlayerManager defaultManager].playlist.count);
    return [PlayerManager defaultManager].playlist.count;
//    return _total;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    PlaylistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
    if (cell == nil) {
        cell = [[PlaylistTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseIdentifier"];
//       cell.alpha = 0;
        
            }
    
    MusicInfo *musicInfo = [PlayerManager defaultManager].playlist[indexPath.row];
    [cell setCellWithMusicInfo:musicInfo];
    
//    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"girl7.jpg"]];
//    UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"girl7.jpg"]];
//    [cell.contentView addSubview:imageView];
    
    // 去掉cell点击的黑框
    UIView *view = [[UIView alloc]initWithFrame:cell.selectedBackgroundView.frame];
    view.backgroundColor = [UIColor clearColor];
    cell.selectedBackgroundView = view;
    cell.backgroundColor = [UIColor clearColor];
    
    
    // 当前歌词高亮
    cell.NameLabel.highlightedTextColor = [UIColor blackColor];
    cell.singerNameLabel.highlightedTextColor = [UIColor blackColor];
    cell.singerImage.highlighted = YES ;
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [[PlayerManager defaultManager] parpreNextMuesicWihtIndex:indexPath.row];
    PlayerViewController * playerVC = [PlayerViewController playerViewControllerManager];
    playerVC.index = indexPath.row;
    [self.navigationController pushViewController:playerVC animated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return [PlaylistTableViewCell setWithHight];

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
